<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\app;


use app\adminapi\logic\sign_in\SignInRewardLogic;
use app\adminapi\logic\calendar\CalendarLogic;
use app\adminapi\logic\task\TaskLogic;
use app\common\model\auth\AdminApp;
use app\common\model\app\App;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * App逻辑
 * Class AppLogic
 * @package app\adminapi\logic
 */
class AppLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 18:09
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $app = App::create([
                'app_name'          => $params['app_name']??'',
                'entity'        => $params['entity']??'',
                'package_name'  => $params['package_name'] ?? '',
                'app_id' => $params['app_id']??'',
                'app_secret' => $params['app_secret']??'',
                'version'       => $params['version'] ?? '',
                'online_status' => $params['online_status']??1,
            ]);

            $appId = $app->id;
            // 复制平台任务
            TaskLogic::copyAppTask($appId);
//             复制平台签到
            SignInRewardLogic::copyAppSign($appId);

            // 复制莫忘日历
//            CalendarLogic::copyPlatformCalendar($appId);

            Db::commit();

            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());

            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 18:09
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            App::where('id', $params['id'])->update([
                'app_name'          => $params['app_name']??'',
                'entity'        => $params['entity']??'',
                'package_name'  => $params['package_name'] ?? '',
                'app_id' => $params['app_id']??'',
                'app_secret' => $params['app_secret']??'',
                'version'       => $params['version'] ?? '',
                'online_status' => $params['online_status']??1,
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 18:09
     */
    public static function delete(array $params): bool
    {
        return App::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/26 18:09
     */
    public static function detail($params): array
    {
        $result = App::findOrEmpty($params['id'])->toArray();
        return $result;
    }

    /*
     * 后台筛选快应用时使用
     * */
    public static function getAll($adminId): array
    {
        $where   = [];
        $where[] = ['status', '=', 1];
        if ($adminId != 1) {
            $app_id = AdminApp::where('admin_id', $adminId)->column('app_id');
            $where[]  = ['id', 'in', $app_id];
        }
        return App::where($where)->field('id,name,online_status')->order('id asc')->select()->toArray();
    }

    /**
     * @notes  更改状态
     * @param array $params
     * @return bool
     * @author heshihu
     * @date 2022/2/21 18:04
     */
    public static function updateStatus(array $params)
    {
        App::update([
            'id'     => $params['id'],
            'status' => $params['status']
        ]);
        return true;
    }
    /**
     * @notes  更改上线状态
     * @param array $params
     * @return bool
     * @author heshihu
     * @date 2022/2/21 18:04
     */
    public static function updateOnlineStatus(array $params)
    {
        App::update([
            'id'     => $params['id'],
            'online_status' => $params['online_status']
        ]);
        return true;
    }
}
